﻿#region Copyright Notice
// 
// 
//  
//  Copyright (c) 2009-2011 Anindya Chatterjee
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//      http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// 
// 
#endregion
using System;
using System.Collections.Generic;
using AbstractClass.Collections.ObjectModel;
using AbstractClass.Extensions;

namespace AbstractClass.Linq
{
    /// <summary>
    /// Provides a set of static methods for querying objects that implement <see cref="ICollection{T}"/>.
    /// </summary>
    public static class CollectionExtensions
    {
        /// <summary>
        /// Converts a collection of elements of type <typeparamref name="T"/> to a collection of elements of
        /// type <typeparamref name="TResult"/> by casting each element of <paramref name="source"/> from 
        /// <typeparamref name="T"/> to <typeparamref name="TResult"/>.
        /// </summary>
        /// <typeparam name="T">The type of the element of <paramref name="source"/>.</typeparam>
        /// <typeparam name="TResult">The type of the element of result collection.</typeparam>
        /// <param name="source">The collection of elements of type <typeparamref name="T"/>.</param>
        /// <returns>A collection of elements of type <typeparamref name="TResult"/>.</returns>
        public static ICollection<TResult> Cast<T, TResult>(this ICollection<T> source)
        {
            source.ThrowIfNullOrEmpty("source");
            Type[] types = {typeof (TResult)};
            types.Add(types.GetType());
            Type sourceType = source.GetType().GetGenericTypeDefinition();
            if (sourceType != null)
            {
                Type constructed = sourceType.MakeGenericType(types);
                var result = (ICollection<TResult>) Activator.CreateInstance(constructed);
                foreach (T element in source)
                {
                    result.Add(element.CastTo<T, TResult>());
                }
                return result;
            }
            return null;
        }


        /// <summary>
        /// Copies all elements of a collection <paramref name="source"/> to another collection
        /// <paramref name="destination"/>.
        /// </summary>
        /// <typeparam name="T">Type of the elements of the collection.</typeparam>
        /// <param name="source">The source collection.</param>
        /// <param name="destination">The destination collection.</param>
        public static void CopyTo<T>(this ICollection<T> source, ICollection<T> destination)
        {
            foreach (T element in source)
            {
                destination.Add(element);
            }
        }

        /// <summary>
        /// Converts a collection to a <see cref="NotifiableCollection&lt;T&gt;"/>.
        /// </summary>
        /// <typeparam name="T">Type of the element of <paramref name="collection"/>.</typeparam>
        /// <param name="collection">The collection to convert to.</param>
        /// <returns>A <see cref="NotifiableCollection&lt;T&gt;"/> containing all elements of <paramref name="collection"/>.</returns>
        public static NotifiableCollection<T> ToNotifiable<T>(this ICollection<T> collection)
        {
            collection.ThrowIfNullOrEmpty("collection");
            var notifiable = new NotifiableCollection<T>(collection);
            return notifiable;
        }
    }
}